package com.wpc.service;

import java.util.Map;

import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.wpc.mapper.UserGzhMapper;
import com.wpc.mapper.UserMapper;
import com.wpc.mapper.WxkeyMapper;
import com.wpc.model.UserAcc;
import com.wpc.model.Wxkey;

@Service
public class WxkeyService {

	@Autowired
	private UserMapper userMapper;
	
	@Autowired
	private UserGzhMapper userGzhMapper;
	
	@Autowired
	private WxkeyMapper wxkeyMapper;
	
	public Map<String,Object> getWxKey(Map<String,Object> map,Integer uid,String sign,String biz){
		map.put("code", "200");//wxkey,uin
		/**
		 * 判断用户是否正确
		 */
		UserAcc user= userMapper.getUserKey(uid);
		if(user==null || user.getProtype()<2){//用户不存在或者未开通任何服务
			map.put("code", "300");//wxkey,uin
			return map;
		}
		/**
		 * 判断签名是否正确
		 */
		String md5Sign=DigestUtils.md5Hex(uid+user.getMd5key());
		if(!md5Sign.equals(sign)){
			map.put("code", "406");//wxkey,uin
			return map;
		}
		/**
		 * 判断公众号是否位此用户订阅的
		 */
		if(userGzhMapper.getUserBizCount(biz, uid)==0){
			map.put("code", "407");//wxkey,uin
			return map;
		}
		Wxkey key =wxkeyMapper.getWxkey();
		map.put("wxkey",key.getWnkey());//wxkey,uin
		map.put("uin", key.getUin());//wxkey,uin
		return map;
	}
}
